package com.huikeportal.controller;

import java.net.URLDecoder;
import java.util.List;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.UserManagerInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.UserAPIService;
import com.huikeportal.service.UserService;

@Controller
public class UserAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private UserAPIService userAPIService;

	@Autowired
	private UserService userService;

	/**
	 * 获取用户列表
	 * 
	 * @param userName
	 * @param source
	 * @param roleId
	 * @param status
	 * @param realName
	 * @return
	 */
	@RequestMapping(value = "/huike_getUserList", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUserList(String userName, String source, String roleId,
			String status, String realName) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getUserList");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userName == null || "".equals(userName) || source == null
				|| "".equals(source)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			UserManagerInfo userManagerInfo = new UserManagerInfo();
			userManagerInfo.setUserName(userName);
			if (roleId != null && !"".equals(roleId)) {
				userManagerInfo.setRoleId(roleId);
			}
			if (status != null && !"".equals(status)) {
				userManagerInfo.setUserStatus(status);
			}
			if (realName != null && !"".equals(realName)) {
				realName = URLDecoder.decode(realName, "UTF-8");
				userManagerInfo.setRealName(realName);
			}
			List<UserManagerInfo> userList = userAPIService.getUserList(
					userManagerInfo, source);
			msgResponse.setObject(userList);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 新增用户提交
	 * 
	 * @param createUserName
	 * @param userName
	 * @param realName
	 * @param cellphone
	 * @param remark
	 * @param roleId
	 * @param email
	 * @param phoneno
	 * @return
	 */
	@RequestMapping(value = "/huike_addUser", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String addUser(String createUserName, String userName,
			String realName, String cellphone, String remark, String roleId,
			String email, String phoneno) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_addUser");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userName == null || "".equals(userName) || createUserName == null
				|| "".equals(createUserName) || realName == null
				|| "".equals(realName) || roleId == null || "".equals(roleId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			boolean flag = userService.selectUserExists(userName);
			// 如果校验用户名重复
			if (!flag) {
				msgResponse.setRespCode("960002");
				msgResponse.setRespCodeDes(MessageTool.RESP_960002);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			} else {
				UserManagerInfo userManagerInfo = new UserManagerInfo();
				userManagerInfo.setCreateUserId(createUserName);
				userManagerInfo.setUserName(userName);
				userManagerInfo.setRoleId(roleId);
				userManagerInfo.setRealName(realName);
				userManagerInfo.setEmail(email);
				// 手机
				userManagerInfo.setPhone(cellphone);
				// 电话
				userManagerInfo.setTel(phoneno);
				userManagerInfo.setNote(remark);
				userAPIService.addUser(userManagerInfo);
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 获取用户信息
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/huike_getUserByUserId", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUserByUserId(String userId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getUserByUserId");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		try {
			UserManagerInfo userManagerInfo = userAPIService
					.getUserById(userId);
			msgResponse.setObject(userManagerInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 编辑用户提交
	 * 
	 * @param userId
	 * @param realName
	 * @param cellphone
	 * @param remark
	 * @param roleId
	 * @param email
	 * @param phoneno
	 * @return
	 */
	@RequestMapping(value = "/huike_editUserSub", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String editUserSub(String userId, String realName, String cellphone,
			String remark, String roleId, String email, String phoneno) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_editUserSub");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId) || realName == null
				|| "".equals(realName) || roleId == null || "".equals(roleId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			UserManagerInfo userManagerInfo = new UserManagerInfo();
			userManagerInfo.setUserId(userId);
			userManagerInfo.setRoleId(roleId);
			userManagerInfo.setRealName(realName);
			userManagerInfo.setEmail(email);
			// 手机
			userManagerInfo.setPhone(cellphone);
			// 电话
			userManagerInfo.setTel(phoneno);
			userManagerInfo.setNote(remark);
			userAPIService.updateUserInfo(userManagerInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 更新用户状态
	 * 
	 * @param userId
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/huike_updateUserStatus", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String updateUserStatus(String userId, String type) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_updateUserStatus");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId) || type == null
				|| "".equals(type)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			MessageCommon message = userAPIService.updateUserStatus(userId,
					type);
			if (message.isFlag()) {
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			} else {
				msgResponse.setRespCode("960003");
				msgResponse.setRespCodeDes(MessageTool.RESP_960003);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 获取用户场所权限列表
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/huike_getUserSiteList", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUserSiteList(String userId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getUserSiteList");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			List<SiteAllInfo> siteList = userAPIService.getUserSiteList(userId);
			msgResponse.setObject(siteList);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	// 更新用户场所权限
	public String updateUserSite() {
		return "";
	}

	// 获取用户数据权限列表
	public String getUserDataList() {
		return "";
	}

	// 更新用户数据权限
	public String updateUserData() {
		return "";
	}

	// 获取用户其他权限
	public String getUserOtherPer() {
		return "";
	}

	// 更新用户其他权限
	public String updateUserOtherPer() {
		return "";
	}

}
